home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Technotools
/
Technotools (Chestnut CD-ROM)(1993).ISO
/
lang_c
/
api_shar
/
shareb.pas
< prev
Wrap
Pascal/Delphi Source File
|
1989-01-26
|
4KB
|
173 lines
(****************************************************************
*
* Name: SHAREB
*
* Function: share memory/data among multiple processes
*
* Shows how to: 1. read from and write to shared memory.
* 2. receive from another process the address of shared data.
* 3. control access to shared data via mailbox semaphore.
*
* Written by: Larry Rush, Quarterdeck Office Systems
*
* Contact: Voice: (213) 392-9851, (213) 392-9701
* BBS: (213) 396-3904, (213) 392-2278
* Fax: (213) 399-3802
*
****************************************************************)
program ShareB;
uses DVAPI;
const
(* minimum API version required *)
REQUIRED = $200;
(* arbitrary # times to read/write shared memory *)
REPS = 4;
var
(* API version number *)
version : integer;
(* TFDD text file *)
tfd : text;
(* application handle of other process *)
apphana : ULONG;
(* mail-related variables *)
status : integer;
malptr : pointer;
mallng : integer;
(* read/write loop control variable *)
i : integer;
type
(* type declarations related to shared data *)
(*i* DATATYPE = integer; *i*)
DATATYPE = string[10];
DATAPTR = ^DATATYPE;
(*r* DATATYPE = record *r*)
(*r* link : DATAPTR; *r*)
(*r* lng : integer; *r*)
(*r* data : string[10]; *r*)
(*r* end; *r*)
const
(* constant value to be assigned to shared memory *)
(*i* SHRCONST : DATATYPE = 22222; *i*)
SHRCONST : DATATYPE = ' BBBBB';
(*r* SHRCONST : DATATYPE = ( *r*)
(*r* link : Nil; *r*)
(*r* lng : 22222; *r*)
(*r* data : ' BBBBB' *r*)
(*r* ); *r*)
var
(* pointer to shared data *)
bufptr : DATAPTR;
(* pointer to pointer to shared data *)
bufptrptr : ^DATAPTR;
(* mailbox semaphore controlling access to shared memory *)
sema : ULONG;
const
(* global name of mailbox semaphore *)
name : string = 'Shared Memory Semaphore';
(********************************************************************
* program_body - read, display and modify contents of shared data.
********************************************************************)
procedure program_body;
begin
(* open TFDD *)
tfd_open (tfd,win_me);
(* find named mailbox semaphore *)
sema := mal_sfind (name);
(* read pointer to mailed data *)
status := mal_read (mal_me,malptr,mallng);
(* assign read pointer to a variable which is a pointer to a pointer *)
bufptrptr := malptr;
(* assign pointer to shared data by dereferencing pointer to pointer *)
bufptr := bufptrptr^;
(* get the application task handle of other process *)
apphana := mal_addr (mal_me);
(* loop till handle of other process is no longer valid *)
while (api_isobj (apphana)) do
begin
(* lock semaphore *)
mal_lock (sema);
(* loop REPS times *)
for i := 1 to REPS do
begin
(* read & display current contents & address of shared data *)
(*i* writeln (tfd,bufptr^,' at ',seg (bufptr^),':',ofs (bufptr^)); *i*)
writeln (tfd,bufptr^,' at ',seg (bufptr^),':',ofs (bufptr^));
(*r* with bufptr^ do *r*)
(*r* writeln (tfd,lng,' ',data,' at ',seg (bufptr^),':',ofs (bufptr^)); *r*)
(* modify contents of shared data *)
bufptr^ := SHRCONST;
end;
(* unlock semaphore *)
mal_unlock (sema);
end;
(* close TFDD *)
tfd_close (tfd);
end;
(**********************************************************************
* main - check for DESQview present and enable required extensions.
***********************************************************************)
begin
(* initialize Pascal interfaces and get API version number *)
version := api_init;
(* if DESQview is not running or version is too low, display a message *)
if (version < REQUIRED) then
writeln ('This program requires DESQview version ',REQUIRED div 256,
'.',(REQUIRED mod 256) div 16,(REQUIRED mod 256) mod 16,' or later.')
(* tell DESQview what extensions to enable and start application *)
else
begin
api_level (REQUIRED);
program_body;
end;
(* disable Pascal interfaces and return from program *)
api_exit;
end.